package edu.nyu.hps.architecture;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class InterfaceFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	int num = 0;

	long startTime = System.currentTimeMillis();
	long lastPaintTime = System.currentTimeMillis();

	JFrame frame;
	JLabel testLabel, positionLabel;
	DrawingPanel fieldPanel;

	int turn = 0;
	Graphics g;

	public InterfaceFrame() {

		// Making the frame
		frame = new JFrame("Cat and Mouse");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// frame.setBounds(new Rectangle(0,0,800,800));
		frame.setBounds(0, 0, 1000, 700);
		frame.setLocation(100,50);
		frame.setAlwaysOnTop(true);
		frame.setMinimumSize(new Dimension(1000, 740));
		frame.setMaximumSize(new Dimension(1000, 740));

		g = frame.getContentPane().getGraphics();
		// frame.setSize(new Dimension(800, 800));

		// Drawing the field
		fieldPanel = new DrawingPanel();
		fieldPanel.setBounds(0, 0, 1000, 700);
		// fieldPanel.setDoubleBuffered(true);

		frame.getContentPane().add(fieldPanel);
		frame.getContentPane().validate();

		// Display the window.
		frame.setVisible(true);
	}

	private void update() {
		Component[] c = frame.getContentPane().getComponents();
		for (int i = 0; i < c.length; i++) {
			// System.out.println("Calling repaint for " + c[i].getWidth());
			Rectangle rect;
			rect = c[i].getBounds();
			// c[i].repaint(rect.x, rect.y, rect.width, rect.height);
			c[i].update(c[i].getGraphics());
			c[i].setBounds(rect);

		}
		// System.out.println("Number of components in frame"
		// + frame.getContentPane().getComponentCount());
	}

	public static void main(String[] args) {
		int port = 34444;
		long time = System.currentTimeMillis();
		String catname = null, mousename = null;
		InterfaceFrame iframe = new InterfaceFrame();

//		if (args.length > 0) {
//			port = Integer.parseInt(args[0]);
//		}

		try {
			 Socket client = new Socket("128.122.80.195", port);
//			Socket client = new Socket("localhost", port);
			// iframe.positionLabel.setText("Connected");
			BufferedReader in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));
			String inputLine;

			while (true) {

				// Reading cats move
				while ((inputLine = in.readLine()) == null) {
				}
				 System.out.println(inputLine);
				
				//Seperating initial move from the rest of the move
				if (inputLine.split(":")[0].equals("INITIAL")) {
					
					//Resetting the game
					iframe.fieldPanel.setTurn(0);
					iframe.fieldPanel.setHunt(true);
					iframe.fieldPanel.resetfield();
					
					String catInitialMove = inputLine.split(":")[1];
					String mouseInitialMove = inputLine.split(":")[2];
					iframe.fieldPanel.setPoint(Integer.parseInt(catInitialMove
							.split(",")[0]), Integer.parseInt(catInitialMove
							.split(",")[1]), DrawingPanel.CAT_FIELD);
					iframe.fieldPanel.setPoint(Integer
							.parseInt(mouseInitialMove.split(",")[0]), Integer
							.parseInt(mouseInitialMove.split(",")[1]),
							DrawingPanel.MOUSE_FIELD);
				} else if (inputLine.split(":")[0].equals("NAME")) {
					catname = inputLine.split(":")[1];
					mousename = inputLine.split(":")[2];
					iframe.fieldPanel.catname = catname;
					iframe.fieldPanel.mousename = mousename;

				} else if (inputLine.indexOf("STATUS: CAUGHT") == 0) {
					iframe.fieldPanel.setHunt(false);
					// break;
				} else {
					String[] catMoves = inputLine.split(":");
					for (int i = 0; i < catMoves.length - 1; i++) {
						String[] move = catMoves[i].split(",");
						iframe.fieldPanel.setPoint(Integer.parseInt(move[0]),
								Integer.parseInt(move[1]),
								DrawingPanel.CAT_TRAVELLED_FIELD);
					}
					String[] move = catMoves[catMoves.length - 1].split(",");
					iframe.fieldPanel.setPoint(Integer.parseInt(move[0]),
							Integer.parseInt(move[1]), DrawingPanel.CAT_FIELD);

					// Reading mouse move
					while ((inputLine = in.readLine()) == null) {
					}
					 System.out.println("Expected mouse move: " + inputLine);
					move = inputLine.split(",");
					iframe.fieldPanel
							.setPoint(Integer.parseInt(move[0]), Integer
									.parseInt(move[1]),
									DrawingPanel.MOUSE_FIELD);
					iframe.fieldPanel.increaseTurn();
				}
				iframe.update();

			}
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

}
